import Vue from 'vue'
import api from '../api'
import commonJs from '../common/js/common'
//分页查询总方法
let listCount = 24;
let loadingTime = 500;
/*获取商品列表     B端shopId   C端cscId 社区ID goodsName商品名称*/
function queryGoodsList(pageNo, pageSize, type, store_id = 0, csc_id = 0,goods_name = '', callback) {
	pageNo = parseInt(pageNo);
	pageSize = parseInt(pageSize);

	if (pageNo < 0 || pageSize <= 0) {
		callQueryResult(callback, []);
		return;
	}
	uni.showLoading({
		title: '加载中...'
	})
	if (pageNo == 0) {
		pageNo = 1;
	}
	var totalPagingList = [];
	let post_data = {}

	if(store_id != 0 && csc_id == 0 && goods_name == ''){
		post_data = {shopId:store_id,pageNum:pageNo,pageSize:pageSize}
	}else if(store_id != 0 && csc_id != 0 && goods_name == ''){
		post_data = {shopId:store_id,cscId:csc_id,pageNum:pageNo,pageSize:pageSize}
	}else if(store_id != 0 && csc_id != 0 && goods_name != ''){
		post_data = {shopId:store_id,cscId:csc_id,goodsName:goods_name,pageNum:pageNo,pageSize:pageSize}
	}else if(store_id == 0 && csc_id != 0 && goods_name == ''){
		post_data = {cscId:csc_id,pageNum:pageNo,pageSize:pageSize}
	}else if(store_id == 0 && csc_id != 0 && goods_name != ''){
		post_data = {cscId:csc_id,goodsName:goods_name,pageNum:pageNo,pageSize:pageSize}
	}else if(store_id == 0 && csc_id == 0 && goods_name != ''){
		post_data = {goodsName:goods_name,pageNum:pageNo,pageSize:pageSize}
	}
	api.adminGoods.getAdminGoodsList(post_data)
	.then(res => {
		if(res.code == 200){
			totalPagingList = res.data
			let pageNoIndex = (pageNo - 1) * pageSize;
			let pageNoNum = pageNo * pageSize;
			// if (pageNoIndex + pageSize <= totalPagingList.length) {
			// 	console.log('step1')
			// 	callQueryResult(callback, totalPagingList.splice(pageNoIndex, pageSize));
			// } else if (pageNoIndex < totalPagingList.length) {
			// 	console.log('step2')
			// 	callQueryResult(callback, totalPagingList.splice(pageNoIndex, totalPagingList.length - pageNoIndex));
			// } else {
			// 	console.log('step3')
			// 	callQueryResult(callback, []);
			// }
			if (pageNoNum >= totalPagingList.length) {
				callQueryResult(callback, totalPagingList);
			} else {
				callQueryResult(callback, []);
			}
		}else{
			commonJs.showToast.showNoneToast(res.msg)
		}
	})
	.catch(err => {
		commonJs.showToast.showNoneToast(err)
	})
}
/*B端获取订单列表信息*/
function queryOrderList(pageNo, pageSize, type, callback){
	switch(type){
		case 1:
			//待抢单
			waitingOrderList(pageNo, pageSize,callback)
			break;
		case 2:
			//配送中
			deliveryOrderList(pageNo, pageSize,callback)
			break;
		case 3:
			//已完成订单
			finishOrderList(pageNo, pageSize,callback)
			break;
	}
}
//待抢单订单列表
function waitingOrderList(pageNo, pageSize,callback){
	pageNo = parseInt(pageNo);
	pageSize = parseInt(pageSize);

	if (pageNo < 0 || pageSize <= 0) {
		callQueryResult(callback, []);
		return;
	}
	uni.showLoading({
		title: '加载中...'
	})
	if (pageNo == 0) {
		pageNo = 1;
	}
	var totalPagingList = [];
	api.order.waitingOrderList()
	.then(res => {
		if(res.code == 200){
			totalPagingList = res.data
			console.log(totalPagingList)
			let pageNoIndex = (pageNo - 1) * pageSize;
			if (pageNoIndex + pageSize <= totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, pageSize));
			} else if (pageNoIndex < totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, totalPagingList.length - pageNoIndex));
			} else {
				callQueryResult(callback, []);
			}
		}else{
			commonJs.showToast.showNoneToast(res.msg)
		}
	})
	.catch(err => {
		commonJs.showToast.showNoneToast(err)
	})
}
//配送中订单列表
function deliveryOrderList(pageNo, pageSize,callback){
	pageNo = parseInt(pageNo);
	pageSize = parseInt(pageSize);
	
	if (pageNo < 0 || pageSize <= 0) {
		callQueryResult(callback, []);
		return;
	}
	uni.showLoading({
		title: '加载中...'
	})
	if (pageNo == 0) {
		pageNo = 1;
	}
	var totalPagingList = [];
	api.order.distributionOrderList()
	.then(res => {
		if(res.code == 200){
			totalPagingList = res.data
			console.log(totalPagingList)
			let pageNoIndex = (pageNo - 1) * pageSize;
			if (pageNoIndex + pageSize <= totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, pageSize));
			} else if (pageNoIndex < totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, totalPagingList.length - pageNoIndex));
			} else {
				callQueryResult(callback, []);
			}
		}else{
			commonJs.showToast.showNoneToast(res.msg)
		}
	})
	.catch(err => {
		commonJs.showToast.showNoneToast(err)
	})
}
//已完成订单列表
function finishOrderList(pageNo, pageSize,callback){
	pageNo = parseInt(pageNo);
	pageSize = parseInt(pageSize);
	
	if (pageNo < 0 || pageSize <= 0) {
		callQueryResult(callback, []);
		return;
	}
	uni.showLoading({
		title: '加载中...'
	})
	if (pageNo == 0) {
		pageNo = 1;
	}
	var totalPagingList = [];
	api.order.finishOrderList()
	.then(res => {
		if(res.code == 200){
			totalPagingList = res.data
			console.log(totalPagingList)
			let pageNoIndex = (pageNo - 1) * pageSize;
			if (pageNoIndex + pageSize <= totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, pageSize));
			} else if (pageNoIndex < totalPagingList.length) {
				callQueryResult(callback, totalPagingList.splice(pageNoIndex, totalPagingList.length - pageNoIndex));
			} else {
				callQueryResult(callback, []);
			}
		}else{
			commonJs.showToast.showNoneToast(res.msg)
		}
	})
	.catch(err => {
		commonJs.showToast.showNoneToast(err)
	})
}


/**-----------------------测试数据时用--------------------------**/
function queryList(pageNo, pageSize, type, callback) {
	pageNo = parseInt(pageNo);
	pageSize = parseInt(pageSize);

	if (pageNo < 0 || pageSize <= 0) {
		callQueryResult(callback, []);
		return;
	}
	uni.showLoading({
		title: '加载中...'
	})
	if (pageNo == 0) {
		pageNo = 1;
	}
	var totalPagingList = [];
	for (let i = 0; i < listCount; i++) {
		var item = {
			'title': (i + 1).toString(),
			'detail': '测试信息' + type
		};
		totalPagingList.push(item);
	}
	let pageNoIndex = (pageNo - 1) * pageSize;
	if (pageNoIndex + pageSize <= totalPagingList.length) {
		callQueryResult(callback, totalPagingList.splice(pageNoIndex, pageSize));
	} else if (pageNoIndex < totalPagingList.length) {
		callQueryResult(callback, totalPagingList.splice(pageNoIndex, totalPagingList.length - pageNoIndex));
	} else {
		callQueryResult(callback, []);
	}
}
function callQueryResult(callback, arg) {
	setTimeout(() => {
		uni.hideLoading();
		callback(arg);
	}, loadingTime)
}

module.exports = {
	queryList,queryGoodsList,queryOrderList
}
